package com.lightsaber.trade.as.web.endorsement;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.as.entity.category.Category;
import com.lightsaber.trade.as.entity.planning.Planning;
import com.lightsaber.trade.as.entity.planning.PlanningStatus;
import com.lightsaber.trade.as.service.planning.PlanningService;
import com.lightsaber.trade.as.service.planning.PreviewEndorsedManager;
import com.lightsaber.trade.as.service.planning.PreviewWaitingForEndorsementManager;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/endorsement")
@Result(name = "PREVIEW", location = "endorsement-plan-first-preview.jsp")
public class PreviewLevelOnePlanningAction extends BaseAction implements SessionAware {
    private final static Logger LOG = LoggerFactory.getLogger(PreviewLevelOnePlanningAction.class);
    private static final long serialVersionUID = 3894011756180097226L;

    private static final String PREVIEW = "PREVIEW";

    protected Map<String, Object> session;
    public static final String SUCESSFULLY = "<Grid><IO Result='0'/></Grid>";

    @Override
    public void setSession(final Map<String, Object> session) {
        this.session = session;
    }

    @Autowired
    private transient PreviewWaitingForEndorsementManager previewWaitingForEndorsementManager;
    @Autowired
    private transient PreviewEndorsedManager previewEndorsedManager;

    @Autowired
    private PlanningService planningService;

    private String planningId;
    private String treeGridData;
    private Category category;
    private String planningStatus;

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        Planning planning = planningService.get(planningId);
        planningStatus = planning.getStatus().toString();
        category = planning.getCategory();

        return PREVIEW;
    }

    public String endorse() throws Exception {

        try {
            Map<String, String> endorseMap = getEndorseMap();

            previewWaitingForEndorsementManager.endorse(planningId, endorseMap);

            this.session.remove(SelectFirstLevelToEndorseAction.ENDORSE_MAP);

            addActionMessage(getText(PROCESSED_SUCCESS));

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            this.addActionError(e.getMessage());
        }

        return list();
    }

    public void data() throws Exception {
        Planning planning = planningService.get(planningId);

        if (planning.getStatus() == PlanningStatus.Endorsed) {

            treeGridData = previewEndorsedManager.preview(planning);

        } else {

            Map<String, String> endorseMap = getEndorseMap();
            // 1. preview planning which waiting for endorsement
            treeGridData = previewWaitingForEndorsementManager.preview(planningId, endorseMap);

        }

        Struts2Utils.renderXml(treeGridData);
    }

//    public String saveData() throws Exception {
//        previewWaitingForEndorsementManager.saveData(treeGridData);
//        Struts2Utils.renderXml(SUCESSFULLY);
//        addActionMessage(getText(PROCESSED_SUCCESS));
//        return null;
//    }

    public String getPlanningId() {
        return planningId;
    }

    public void setPlanningId(final String planningId) {
        this.planningId = planningId;
    }

    public String getTreeGridData() {
        return treeGridData;
    }

    public void setTreeGridData(final String treeGridData) {
        this.treeGridData = treeGridData;
    }

    private Map<String, String> getEndorseMap() {
        @SuppressWarnings("unchecked")
        Map<String, String> endorseMap = (Map<String, String>) this.session
                .get(SelectFirstLevelToEndorseAction.ENDORSE_MAP);

        if (endorseMap == null) {
            endorseMap = new HashMap<String, String>();
            this.session.put(SelectFirstLevelToEndorseAction.ENDORSE_MAP, endorseMap);
        }

        return endorseMap;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(final Category category) {
        this.category = category;
    }

    public String getPlanningStatus() {
        return planningStatus;
    }

    public void setPlanningStatus(final String planningStatus) {
        this.planningStatus = planningStatus;
    }

}
